package com.homer.gps.track.persist;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper{

	// name of the database file for your application -- change to something appropriate for your app
	private static final String DATABASE_NAME = "DemoORM.db";
	// any time you make changes to your database objects, you may have to increase the database version
	private static final int DATABASE_VERSION = 2;

	public DatabaseHelper(final Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// the DAO object we use to access the SimpleData table
	private Dao<RouteModel, Integer> routeDao = null;
	private Dao<PointModel, Integer> pointDao = null;
	private Dao<PlaceModel, Integer> placeDao = null;
	private Dao<StopModel, Integer> stopDao = null;
	private Dao<StopsHasPlacesModel, Integer> stophasplacesDao = null;

	@Override
	public void onCreate(final SQLiteDatabase db, final ConnectionSource connectionSource) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, RouteModel.class);
			TableUtils.createTable(connectionSource, PointModel.class);
			TableUtils.createTable(connectionSource, PlaceModel.class);
			TableUtils.createTable(connectionSource, StopModel.class);
			TableUtils.createTable(connectionSource, StopsHasPlacesModel.class);
		}
		catch (final SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * This is called when your application is upgraded and it has a higher version number. This allows you to adjust the various data to
	 * match the new version number.
	 */
	@Override
	public void onUpgrade(final SQLiteDatabase db, final ConnectionSource connectionSource, final int oldVersion, final int newVersion) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, RouteModel.class, true);
			TableUtils.dropTable(connectionSource, PointModel.class, true);
			TableUtils.dropTable(connectionSource, PlaceModel.class, true);
			TableUtils.dropTable(connectionSource, StopModel.class, true);
			TableUtils.dropTable(connectionSource, StopsHasPlacesModel.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		}
		catch (final SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}
	
	public Dao<RouteModel, Integer> getRouteDao() throws SQLException {
		if (this.routeDao == null) {
			this.routeDao = getDao(RouteModel.class);
		}
		return this.routeDao;
	}
	
	public Dao<PointModel, Integer> getPointDao() throws SQLException {
		if (this.pointDao == null) {
			this.pointDao = getDao(PointModel.class);
		}
		return this.pointDao;
	}
	
	public Dao<PlaceModel, Integer> getPlaceDao() throws SQLException {
		if (this.placeDao == null) {
			this.placeDao = getDao(PlaceModel.class);
		}
		return this.placeDao;
	}
	
	public Dao<StopModel, Integer> getStopDao() throws SQLException {
		if (this.stopDao == null) {
			this.stopDao = getDao(StopModel.class);
		}
		return this.stopDao;
	}
	
	public Dao<StopsHasPlacesModel, Integer> getStopsHasPlacesDao() throws SQLException {
		if (this.stophasplacesDao == null) {
			this.stophasplacesDao = getDao(StopsHasPlacesModel.class);
		}
		return this.stophasplacesDao;
	}

	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
		this.routeDao = null;
		this.pointDao = null;
		this.placeDao = null;
		this.stopDao = null;
		this.stophasplacesDao = null;
	}

}
